package com.tpay.client.presenter;

import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ContentSlot;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.annotations.UseGatekeeper;
import com.gwtplatform.mvp.client.proxy.Place;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.gwtplatform.mvp.client.proxy.RevealContentHandler;
import com.gwtplatform.mvp.client.proxy.RevealRootContentEvent;
import com.tpay.client.CurrentUser;
import com.tpay.client.NameTokens;
import com.tpay.client.event.LoginAuthenticatedEvent;
import com.tpay.client.uihandlers.LoginPageUiHandlers;
import com.tpay.client.uihandlers.MainPageUiHandlers;
import com.tpay.shared.action.LoginAction;
import com.tpay.shared.action.LoginResult;

public class MainPagePresenter extends Presenter<MainPagePresenter.MyView, MainPagePresenter.MyProxy> implements MainPageUiHandlers {
	@ProxyCodeSplit
	@NameToken(NameTokens.mainPage)
	@UseGatekeeper(LoggedInGatekeeper.class)
	public interface MyProxy extends Proxy<MainPagePresenter>, Place {
	}
	
	public interface MyView extends View, HasUiHandlers<MainPageUiHandlers> {
	}

	private final PlaceManager placeManager;
	
	@ContentSlot
	public static final Type<RevealContentHandler<?>> TYPE_SetContextAreaContent = new Type<RevealContentHandler<?>>();
	
	@Inject
	public MainPagePresenter(EventBus eventBus, MyView view, MyProxy proxy, PlaceManager placeManager) {
		super(eventBus, view, proxy);

		this.placeManager = placeManager;
		
		this.getView().setUiHandlers(this);
	}

	@Override
	protected void revealInParent() {
		RevealRootContentEvent.fire(this, this);
	}

	@Override
	public void prepareFromRequest(PlaceRequest request) {
		super.prepareFromRequest(request);
		
		String nameToken = request.getParameter(LoginPagePresenter.REDIRECT, NameTokens.dashboard);
		
		// TODO 나중에 아래 두 줄을 풀어야 함	
//		PlaceRequest nestedRequest = new PlaceRequest(nameToken);
//		placeManager.revealPlace(nestedRequest);
	}
}
